AstVerb.php
<?php
namespace Tlf\Scrawl\Ext\MdVerb;
class Ast {
public \Tlf\Scrawl $scrawl;
public function __construct($scrawl){
$this->scrawl = $scrawl;
}
public function get_markdown($key, $template='ast/default'){
$value = $this->get_ast($key);
$template = $this->scrawl->get_template($template, [$key, $value, $this]);
return $template;
}
public function get_ast(string $key, int $length=-1){
$parts = explode('.',$key);
if ($length!=-1){
$parts = array_slice($parts, 0,$length);
}
$group = array_shift($parts);
$class = array_shift($parts);
$ast = $this->scrawl->get('ast', $group.'.'.$class);
$stack = $group.'.'.$class;
$next = $ast;
foreach ($parts as $p){
$current = $next;
$stack .= '.'.$p;
if (!isset($current[$p])&&is_array($current)){
foreach ($current as $i=>$item){
if (!is_numeric($i))continue;
if ($item['name']==$p){
$next = $item;
continue 2;
}
}
print_r($current);
echo "\n\nFailed at ".$stack;
echo "\n\nsomething went wrong\n\n";
exit;
} else if (!isset($current[$p])){
echo "\n\ncan't get next item\n\n";
exit;
}
$next = $current[$p];
}
return $next;
}
public function getVerbs(): array{
return [
'ast'=>'verbAst',
'classMethods'=>'getClassMethodsTemplate',
'ast_class'=> 'getAstClassInfo',
];
}
public function getAstClassInfo(array $info, string $fqn, string $dotProperty){
$class = $this->scrawl->getOutput('astClass', $fqn);
if ($class == 'null') return "class '$fqn' not found in ast.";
$propStack = explode('.', $dotProperty);
$head = $class;
if (!is_array($head)){
$file = $info['file']->path;
$this->scrawl->error("@ast($fqn, $dotProperty) failed", 'in '.$file);
return "@ast($fqn) failed";
}
foreach ($propStack as $prop){
if ($prop=='*'){
return print_r($head,true);
}
if (!isset($head[$prop])){
$options = [];
foreach ($head as $key=>$value){
if (is_numeric($key) && ($value['name']??null)==$prop){
$head = $head[$key];
continue 2;
} else if (is_numeric($key) && isset($value['name'])){
$options[] = $value['name'];
}
}
$options = array_merge($options, array_keys($head));
$msg = "Cannot find '$prop' part of '$dotProperty' on '$fqn'. You may try one of: ". implode(", ", $options);
$this->scrawl->error('@ast or @ast_class', $msg);
return $msg;
}
$head = $head[$prop];
}
if (is_array($head)){
if (isset($head['body']))return $head['body'];
else if (isset($head['description']))return $head['description'];
else if (isset($head['src']))return $head['src'];
$msg="Found an array for '$dotProperty' on '$fqn' with keys: ".implode(", ", array_keys($head));
$this->scrawl->error('@ast or @ast_class', $msg);
return $msg;
}
return $head;
}
public function verbAst($info, $className, $dotProperty){
return $this->getAstClassInfo($info, $className, $dotProperty);
}
public function getClassMethodsTemplate($verb, $argListStr, $line){
if ($verb!='classMethods')return;
$args = explode(',', $argListStr);
$className = $args[0];
$visibility = '*';
if (isset($args[1])){
$visibility = trim($args[1]);
}
$class = $this->scrawl->getOutput('astClass', $className);
$template = dirname(__DIR__,2).'/Template/classMethods.md.php';
ob_start();
require($template);
$final = ob_get_clean();
return $final;
}
}